import json
from urllib.parse import urlparse, parse_qs

import requests
from bs4 import BeautifulSoup

url = "https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.1.7e135399Joe28j&id=590655880239&skuId=4222870978770&areaId=440100&user_id=2200715826810&cat_id=2&is_b=1&rn=0965179336bd18b94a5c6febc3d09096"

header = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
}

res = requests.get(url,headers= header)
html = BeautifulSoup(res.text,'lxml')
formList = html.find('form',{'id':'J_FrmBid'})
title = formList.find('input',{'name':'title'}).get("value")
shopname = formList.find('input',{'name':'seller_nickname'}).get("value")
addr = formList.find('input',{'name':'region'}).get("value")
print(title,shopname,addr)

liList = html.find('ul',{'id':'J_AttrUL'}).find_all("li")
brandname = html.find('ul',{'id':'J_AttrUL'}).find('li',{'id':'J_attrBrandName'})['title'].strip()
for li in liList:
    if "产品名称" in li.text:
        name = li['title'].strip()
    if "型号" in li.text:
        model = li['title'].strip()
    if "生产企业" in li.text:
        companyname = li['title'].strip()
print(brandname,name,model,companyname)

def getUrlParam(url,name):
    query = urlparse(url).query
    res = dict(parse_qs(query).items())
    return res[name][0]
itemId= getUrlParam(url,"id")
user_id = getUrlParam(url,"user_id")
commendUrl = "https://rate.tmall.com/list_detail_rate.htm"

header = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
    "referer":"https://detail.tmall.com/item.htm",
    "cookie":"cna=h7/WExR98HMCAbck5+fn2y1y; x=__ll%3D-1%26_ato%3D0; _m_h5_tk=076ba86f8d9524f15984971fd99cbd09_1566015258288; _m_h5_tk_enc=30160c247cc5f2ac08980a480cc98830; hng=CN%7Czh-CN%7CCNY%7C156; uc3=lg2=WqG3DMC9VAQiUQ%3D%3D&nk2=AHYzkqrWU969%2FA6z&id2=UNDQTf%2BC0nyNhA%3D%3D&vt3=F8dBy3Kw2V4Uhvyq77Y%3D; t=0963ac6be3d67c0fedd81a404a7c94ef; tracknick=clq516138253; lid=clq516138253; uc4=id4=0%40UgcgsjW5sUYDTYseVCX%2BjQInX5ex&nk4=0%40AhhOD7FH%2BCboOdK55orpMefb9KrAesc%3D; lgc=clq516138253; enc=YfEO5fwsu8ngiigJcbnPQNse41%2BZD4FI47QjkHKd1w4kUzojFxac6jOlAM6hXEaZ3krHP5L%2BvTsGvnDZuWG56Q%3D%3D; _tb_token_=353eb76e01ef6; cookie2=1b6dc40ae44f708671d356c8bb7b2120; otherx=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0; x5sec=7b22726174656d616e616765723b32223a223030366666353762383266376366343730626636326438353161373262633032434d61462b4f6f46454c6d376d4f6d74746f2f6c7777453d227d; l=cBxildhcqi69XC4vBOCwVQKbL8btpKdfguSJGCx6i_5p5186HcbOkJyLheJ6cjWhtCTB4iueOIwtWFK4JI70xZyCwTvQb; isg=BM_PGk5-zpivpMro9YwcU7fkTGN15CL0YtSNpOHcXT5EsO6y6cZ0ZsmmsqCryPuO"
}
params = {
    "itemId":itemId,
    # "callback":"jsonp741",
    "sellerId":user_id
    # "sellerId":
}
res = requests.get(commendUrl, params,headers=header).text[11:-1]
resJson = json.loads(res)
rateList = resJson["rateDetail"]['rateList']
# print(rateList)
for item in rateList:
    rateContent = item['rateContent']
    username = item['displayUserNick']
    print(username,rateContent)


